package com.zoe.tree.entity;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author zbg
 * @date 2023/06/20
 * @description $description
 */

/**
 * 部门管理
 */
@Data
@TableName(value = "sys_dept")
public class SysDept implements Comparable<SysDept> {
    /**
     * 部门ID
     */
    @TableId(value = "dept_id", type = IdType.ASSIGN_ID)
    private Long deptId;

    /**
     * 父部门ID
     */
    @TableField(value = "parent_id")
    private Long parentId;

    /**
     * 部门名称
     */
    @TableField(value = "`name`")
    private String name;

    /**
     * 排序值
     */
    @TableField(value = "sort")
    private Integer sort;

    @TableField(exist = false)
    private Set<SysDept> children;

    @TableField(exist = false)
    private Long nextId;

    public void addChildren(SysDept sysDept) {
        if (children == null) {
            children = new TreeSet<>();
        }
        children.add(sysDept);
    }

    @Override
    public int compareTo(SysDept sysDept) {
        if (ObjectUtil.equal(this.getSort(), sysDept.getSort())) {
            return 1;
        }
        return this.getSort() - sysDept.getSort();
    }
}